Pascal’s Triangle II

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:

Could you optimize your algorithm to use only O(k) extra space?

Solution:

  1. public class Solution {
  2. public List<Integer> getRow(int k) {
  3. Integer[] arr = new Integer[k + 1];
  4. Arrays.fill(arr, 0);
  5. arr[0] = 1;
  6. for (int i = 1; i <= k; i++) {
  7. for (int j = i; j > 0; j--) {
  8. arr[j] = arr[j] + arr[j - 1];
  9. }
  10. }
  11. return Arrays.asList(arr);
  12. }
  13. }